package com.zsrt.oa.user.dao;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import com.zsrt.oa.user.entity.UserEntity;
import com.zsrt.oa.user.utils.DBUtil;

public class Dao01 implements IUserDao{

	@Override
	public void add(UserEntity entity) throws Exception {
		
	}

	@Override
	public void del(UserEntity entity) throws Exception {
		
	}

	@Override
	public int update(UserEntity search, UserEntity newEntity) throws Exception {
		//反射：获取对象属性，方法，方法的调用
		//1.加载驱动
		//2.新建连接
		Connection conn = DBUtil.getConnection();
		//3.创建sql语句
		String sql = "UPDATE oa_user_t set password = ?,name = ?,phone = ?"
				+ ",birthday = ? ,punch_time = ? WHERE id = ?;";
		
		Method[] methods = newEntity.getClass().getDeclaredMethods();
		
		
		//4.执行sql语句
		PreparedStatement ps = conn.prepareStatement(sql);
		//-----------------------
		String reg = "get.*";
		int temp = 1;
		for(int index = 0;index < methods.length;index++){
			String methodName = methods[index].getName();
			if(!methodName.equalsIgnoreCase("getid") && methodName.matches(reg)){
				ps.setString(temp++, (String) methods[index].invoke(newEntity));
				
			}
		}
		
		ps.setInt(6, search.getId());
		
		int counter = ps.executeUpdate();
				
		//5.关闭连接
		ps.close();
		conn.close();
		System.out.println();
		return counter;
	}

	@Override
	public UserEntity query(Integer id) throws Exception {
		//1.加载驱动
		//2.新建连接
		Connection conn = DBUtil.getConnection();
		//3.创建sql语句
		String sql = "SELECT ID AS id,NAME AS name,PASSWORD AS password,"
				+ "PHONE AS phone,BIRTHDAY AS birthday,PUNCH_TIME AS punchTime"
				+ " FROM oa_user_t  WHERE id = ?;";
		
		//4.执行sql语句
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		ResultSetMetaData metaData = rs.getMetaData();
		//-----------------------
		UserEntity entity = new UserEntity();
//		Class.forName("com.zsrt.oa.user.entity.UserEntity");
		Method[] methods = entity.getClass().getDeclaredMethods();
		while(rs.next()){
			//----
			int index = 1;		//遍历字段
			while(index <= metaData.getColumnCount()){
//				System.out.println(metaData.getColumnName(index)+":"+rs.getString(index++));
				
				for(Method method:methods){
					String methodName = method.getName();
					//对每个方法比较，找出set方法
					if(methodName.equalsIgnoreCase("set"+removeBottom(metaData.getColumnName(index)))){
//						Class<?>[] parameterTypes = method.getParameterTypes();
//						System.out.println(method.getParameterTypes()[0].getName());
						Class<?> forName = Class.forName(method.getParameterTypes()[0].getName());
						if(forName == Integer.class){
							method.invoke(entity,Integer.parseInt(rs.getString(metaData.getColumnName(index++))));
						}else{
							method.invoke(entity,rs.getString(removeBottom(metaData.getColumnName(index++))));
						}
//						System.out.println(index++);
//						index++;
						break;
						//1.int
						//2.double
						//3.String
//						method.invoke(entity, rs.getString(index++));
					}
				}
				
			}
		}
		//5.关闭连接
		return entity;
	}
	
	//去掉下划线
	public static String removeBottom(String name){
		String[] strArray = name.split("_");
		String newName = "";
		for(String str:strArray){
//			System.out.println(str+" ");
			newName += str;
		}
		return newName;
	}

}
